package com.nit.mapper;

import com.nit.dto.StockOutDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;

import java.util.List;

public interface StockOutMapper {
    @Select("<script> " +
            "SELECT COUNT(*) " +
            "FROM\n" +
            "\ttb_stock_out,\n" +
            "\ttb_goods\n" +
            "WHERE\n" +
            "\ttb_stock_out.shop_id IN (\n" +
            "\t\tSELECT\n" +
            "\t\t\ttb_shop.shop_id\n" +
            "\t\tFROM\n" +
            "\t\t\ttb_shop\n" +
            "\t\tWHERE\n" +
            "\t\t\ttb_shop.user_id = #{userId}\n" +
            "\t)\n" +
            "AND tb_stock_out.goods_id = tb_goods.goods_id" +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "<if test='goodsCate != null'> AND goods_cate = #{goodsCate} </if>" +
            "</script> ")
    Long count4Table(@Param("goodsName") Object goodsName,
                     @Param("goodsCate") Object goodsCate,
                     @Param("userId") Object userId);

    @Select("<script> " +
            "SELECT\n" +
            "\ttb_stock_out.shop_id,\n" +
            "\ttb_goods.goods_name,\n" +
            "\ttb_stock_out.gmt_create,\n" +
            "\ttb_stock_out.so_number,\n" +
            "\ttb_stock_out.user_id,\n" +
            "\ttb_stock_out.goods_id\n" +
            "FROM\n" +
            "\ttb_stock_out,\n" +
            "\ttb_goods\n" +
            "WHERE\n" +
            "\ttb_stock_out.shop_id IN (\n" +
            "\t\tSELECT\n" +
            "\t\t\ttb_shop.shop_id\n" +
            "\t\tFROM\n" +
            "\t\t\ttb_shop\n" +
            "\t\tWHERE\n" +
            "\t\t\ttb_shop.user_id = #{userId}\n" +
            "\t)\n" +
            "AND tb_stock_out.goods_id = tb_goods.goods_id" +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "<if test='goodsCate != null'> AND goods_cate = #{goodsCate} </if>" +
            "ORDER BY tb_stock_out.so_id DESC " +
            "LIMIT #{start}, #{length} " +
            "</script> ")
    List<StockOutDTO> list4Table(@Param("start")Integer start,
                                 @Param("length") Integer length,
                                 @Param("goodsName") Object goodsName,
                                 @Param("goodsCate") Object goodsCate,
                                 @Param("userId") Object userId);

    @Select("SELECT user_name FROM tb_user WHERE user_id = #{userId} ")
    String getUserName(@Param("userId")Object userId)throws Exception;


}
